package com.wl.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wl.dao.NewsCategoryDao;
import com.wl.enums.CategoryType;
import com.wl.model.NewsCategory;
import com.wl.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.WeakHashMap;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    public NewsCategoryDao newsCategoryDao;

    @Override
    public List<NewsCategory> list(String title, Long id) {
        EntityWrapper<NewsCategory> wrapper = new EntityWrapper<>();
        if (id != null) {
            wrapper.eq("category_id", id);
        }
        if (title != null && title != "") {

            wrapper.like("title", "%" + title + "%");
        }
        return newsCategoryDao.selectList(wrapper);
    }

    @Override
    public NewsCategory findById(Long id) {
        return newsCategoryDao.selectById(id);
    }

    @Transactional
    @Override
    public void delById(Long id) {
        newsCategoryDao.deleteById(id);
    }

    @Override
    public void updateById(NewsCategory category) {
        newsCategoryDao.updateById(category);
    }

    @Override
    public void add(NewsCategory category) {
        category.setType(CategoryType.common.name());
        newsCategoryDao.insert(category);
    }
}
